package com.lemon.funplus.cheanh;

import yuku.ambilwarna.AmbilWarnaDialog;
import yuku.ambilwarna.AmbilWarnaDialog.OnAmbilWarnaListener;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

import com.lemon.funplus.CheAnhActivity;
import com.lemon.funplus.R;

public class BrushSettingDialog extends DialogFragment {
	static String colorID="color_id";
	static String radiusID="radius_id";
	static String ID="settingid";
	private View root;
	protected LinearLayout brushSizeIndicatorContainer,brushOpacityIndicatorContainer;
	protected BrushIndicatorView brushIndicator;
	protected Paint paint;
	protected SeekBar brushSize,brushOpacity;

	public int color;
	public int radius;
	public int paintAlpha;
	CheAnhActivity anhActivity;

	public static BrushSettingDialog newInstance(CheAnhActivity anhActivity) {
		BrushSettingDialog f = new BrushSettingDialog(anhActivity);
		return f;
	}
	
	public BrushSettingDialog(CheAnhActivity anhActivity) {
		this.anhActivity=anhActivity;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		color=anhActivity.color;
		radius=anhActivity.brushRadius;
		paintAlpha=anhActivity.alpha;
		initViewsSetting();
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		AlertDialog dialog = new AlertDialog.Builder(getActivity()).setTitle("Brush setting").setView(root)
				.setNegativeButton("OK", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						anhActivity.color=color;
						anhActivity.brushRadius=radius;
						anhActivity.alpha=paintAlpha;
						Log.e("", "color: "+color);
						Log.e("", "radius: "+radius);
						Log.e("", "paintAlpha: "+paintAlpha);
						dismiss();
					}
				}).create();
		return dialog;
	}

	private void initViewsSetting() {
		LayoutInflater inflater = LayoutInflater.from(getActivity());
		root = (ViewGroup) inflater.inflate(R.layout.cheanh_brush_setting, null, false);
		brushSizeIndicatorContainer = (LinearLayout) root.findViewById(R.id.brush_size_indicator_container);
		brushOpacityIndicatorContainer = (LinearLayout) root.findViewById(R.id.brush_opacity_indicator_container);
	
		brushIndicator = new BrushIndicatorView(this.getActivity(), this);
		brushSizeIndicatorContainer.addView(brushIndicator);
		brushSize = (SeekBar) root.findViewById(R.id.brush_size);
		brushSize.setOnSeekBarChangeListener(new BrushSizeSeekbarListener(BrushSettingDialog.this,  false));

		brushOpacity = (SeekBar) root.findViewById(R.id.brush_opacity);
		brushOpacity.setOnSeekBarChangeListener(new BrushSizeSeekbarListener(BrushSettingDialog.this,  true));
		brushOpacity.setMax(100);
		brushOpacity.setProgress(getPaintAlpha());
		root.findViewById(R.id.brush_setting_color).setOnClickListener(clickListener);
	}
	OnClickListener clickListener=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.brush_setting_color:
				showColorPicker();
				break;
			default:
				break;
			}
		}
	};
	public void showColorPicker() {
		AmbilWarnaDialog dialog = new AmbilWarnaDialog(getActivity(), BrushSettingDialog.this.color, new OnAmbilWarnaListener() {
			@Override
			public void onOk(AmbilWarnaDialog dialog, int color) {
				BrushSettingDialog.this.color=color;
				updateRadius();
			}

			@Override
			public void onCancel(AmbilWarnaDialog dialog) {

			}
		});
		dialog.show();
	}
	protected void updateSlider() {
		brushSize.setProgress((int) ((radius - 1) * 100.0f) / (brushIndicator.maxRadius - 1));
	}
	protected void updateRadius() {
		brushIndicator.updateRadius();
	}
	protected void setBrushRadius(int rad) {
		radius=rad;
	}
	
	public void setPaintAlpha(int newAlpha){
	    paintAlpha=Math.round((float)newAlpha/100*255);
	    updateRadius();
	}
	public int getPaintAlpha(){
	    return Math.round((float)paintAlpha/255*100);
	}
}
